#!/usr/local/bin/Resource/www/cgi-bin/php
<?php
function str_between($string, $start, $end){
	$string = " ".$string; $ini = strpos($string,$start);
	if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini;
	return substr($string,$ini,$len);
}
error_reporting(0);
exec ("rm -f /tmp/test.xml");
$file = $_GET["file"];
$file=urldecode($file);
$ttxml ="<subtitrare>"."\n";
$full_line = '';
$last_end=0;
$html = file_get_contents($file);
$videos = explode('<p', $html);
unset($videos[0]);
$videos = array_values($videos);
$n=1;
foreach($videos as $video) {
$t1=explode('begin="',$video);
$t2=explode('"',$t1[1]);
$start=$t2[0];
$time1=explode(":",$start);
$begin = 60*$time1[0] + $time1[1];
$t1=explode('end="',$video);
$t2=explode('"',$t1[1]);
$endtime=$t2[0];
$time1=explode(":",$endtime);
$end = 60*$time1[0] + $time1[1];
$f = "/usr/local/bin/home_menu";
if (!file_exists($f)) {
if ($begin > $last_end)
 {
   $ttxml .="<sub>"."\n";
   $ttxml .="<time1>".$last_end."</time1>"."\n";
   $ttxml .="<time2>".$begin."</time2>"."\n";
   $ttxml .="<line1></line1>"."\n";
   $ttxml .="<line2></line2>"."\n";
   $ttxml .="</sub>"."\n\n";
 }
 $last_end=$end;
}
$line=str_between($t1[1],">","</p");
$l=explode("<br/>",$line);
$line1=$l[0];
$line2=$l[1];
$line1 = trim(preg_replace("/<(.*)>|(\{(.*)\})/e","",$line1));
$line2 = trim(preg_replace("/<(.*)>|(\{(.*)\})/e","",$line2));
if ($line2=="")
{
$line2=$line1;
$line1="";
}
$ttxml .="<sub>"."\n";
$ttxml .="<time1>".$begin."</time1>"."\n";
$ttxml .="<time2>".$end."</time2>"."\n";
$ttxml .="<line1>".$line1."</line1>"."\n";
$ttxml .="<line2>".$line2."</line2>"."\n";
$ttxml .="</sub>"."\n\n";
}
$ttxml .="</subtitrare>";
$new_file = "/tmp/test.xml";
$fh = fopen($new_file, 'w');
fwrite($fh, $ttxml);
fclose($fh);
?>
